package com.ellricky.baselib.ui.student;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.ResUtil;
import com.andr.common.util.ViewUtil;
import com.andr.common.widget.tabvp.SimpleTabLayout;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.listener.StuManageListener;
import com.ellricky.baselib.ui.msg.MsgTipActivity;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.EDialogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;


public class StudentManageActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> implements StuManageListener {

    private SimpleTabLayout tabLayout;
    private ViewPager vp;

    private RelativeLayout rlAge;
    private RelativeLayout rlBirth;
    private RelativeLayout rlHour;
    private TextView tvAge;
    private TextView tvBirth;
    private TextView tvClassHour;
    private ImageView ivSearch;
    private EditText etSearch;

    private List<Fragment> frgList;

    private EPickerUtil ePickerUtil;
    private IdNamePickerEntity selAge;

    private String age = "";
    private String birthOrder = "0";
    private String hourOrder = "0";
    private String searchKey = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocalCacheUtil.setIsReloadData(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalCacheUtil.setIsReloadData(false);
    }


    @Override
    public int getContentLayout() {
        return R.layout.base_activity_stu_manage;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        ePickerUtil = new EPickerUtil(this);
        mTvTitle.setText(R.string.base_stu_manage);

        tabLayout = findViewById(R.id.tabLayout);
        vp = findViewById(R.id.vp);

        rlAge = findViewById(R.id.rlAge);
        rlBirth = findViewById(R.id.rlBirth);
        rlHour = findViewById(R.id.rlHour);
        tvAge = findViewById(R.id.tvAge);
        tvBirth = findViewById(R.id.tvBirth);
        tvClassHour = findViewById(R.id.tvClassHour);
        ivSearch = findViewById(R.id.ib_search);
        etSearch = findViewById(R.id.etSearch);

        rlAge.setOnClickListener(this);
        rlBirth.setOnClickListener(this);
        rlHour.setOnClickListener(this);
        ivSearch.setOnClickListener(this);

        initTab();
    }


    private void initTab() {
        frgList = new ArrayList<>();
        frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_VIP).setNumListener(this));
        frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_STOP).setNumListener(this));
        frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_NO).setNumListener(this));
        frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_RETURN).setNumListener(this));
        frgList.add(StudentFragment.newInstance(BaseConstant.STU_TYPE_CHAO).setNumListener(this));

        List<String> tList = Arrays.asList(ResUtil.getStrArr(this, R.array.base_stu_manage));

        tabLayout.setUp(getSupportFragmentManager(), frgList, tList, vp);
    }


    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == rlAge) {
            ePickerUtil.checkAgePicker(selAge, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity nameValuePickerEntity) {
                    selAge = nameValuePickerEntity;
                    if (selAge != null) age = selAge.getName();
                    else age = "";
                    refreshData();
                }
            });
        } else if (v == rlBirth) {
            birthOrder = EAppUtil.getStuOrder(this, tvBirth, birthOrder);
            refreshData();
        } else if (v == rlHour) {
            hourOrder = EAppUtil.getStuOrder(this, tvClassHour, hourOrder);
            refreshData();
        }else if (v ==ivSearch){
            searchKey = etSearch.getText().toString().trim();
            refreshData();
        }
    }

    private void refreshData() {
        if (selAge != null) age = selAge.getId();
        else age = "0";
        getCurFrg().updateFilterParam(age, birthOrder, hourOrder, false,searchKey);
    }


    @Override
    protected void onClickBtnRight() {
        super.onClickBtnRight();
        EDialogUtil.showStuManageDialog(this, new OnTakePhotoListener() {
            @Override
            public void onClickItem(DialogInterface dialogInterface, int item) {
                if (item == 0) EAppUtil.jumpStuApplyList(StudentManageActivity.this);
                else if (item == 1)
                    EAppUtil.jumpPageWithNoParam(StudentManageActivity.this, MsgTipActivity.class);
            }
        });
    }

    @Override
    protected void onClickBtnRightLeft() {
        super.onClickBtnRightLeft();
        EAppUtil.jumpPageWithNoParam(this, StudentSearchActivity.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BaseConstant.RESULT_CODE_ADD) {
            refreshData();
        }
    }

    @Override
    public void updateTabNum(String formalNum, String stopNum, String noNum, String returnNum, String chaoNum) {
        Objects.requireNonNull(tabLayout.getTabAt(0)).setText("会员（" + formalNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(1)).setText("停课（" + stopNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(2)).setText("未续费（" + noNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(3)).setText("退费（" + returnNum + "人）");
        Objects.requireNonNull(tabLayout.getTabAt(4)).setText("超期（" + chaoNum + "人）");
    }

    @Override
    public void resetFilterIcon() {
        age = "";
        birthOrder = "0";
        hourOrder = "0";
        ViewUtil.setTextViewDrawable(this, tvBirth, R.drawable.base_icon_order_nor, 2);
        ViewUtil.setTextViewDrawable(this, tvClassHour, R.drawable.base_icon_order_nor, 2);
    }

    private StudentFragment getCurFrg() {
        int index = tabLayout.getSelectedTabPosition();
        return (StudentFragment) frgList.get(index);
    }
}

